package com.cn.shop;

import com.cn.shop.utils.CheckException;
import com.cn.shop.utils.ResultBean;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 错误统一处理类
 *
 * @author suhao
 * @time 2018-01-21
 * @email suhao755@gmail.com
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = CheckException.class)
    @ResponseBody
    public ResultBean<String> jsonErrorHandler(HttpServletRequest req, CheckException e) throws Exception {
        ResultBean<String> result = new ResultBean<>();
        result.setMsg(e.getMessage());
        result.setCode(ResultBean.FAIL);
        result.setData("Some Data");
        return result;
    }
}